/**
 * Created by msi on 2016/11/22.
 */
$(function () {
    //白菜价公用ajax模板
    var BcjAjax = (function () {
        var Bcjdata;

        function ajax(url, callback) {
            if (Bcjdata) {
                return false;
            }
            $.ajax({
                url: url,
                type: "get",
                dataType: "json",
                data: {},
                success: function (data) {
                    Bcjdata = data;
                    callback && callback(data);
                },
                error: function () {
                    alert("服务器端口错误，请联系管理员")
                }
            })
        }

        return ajax;
    })()

//请求首页导航数据
    BcjAjax(URL.getbaicaijiatitle, function (data) {
        //console.log(data);
        var html = template("Bcj_art", data);
        $(".Bcj_nav ul").html(html);

        //加载完成后，设置tab栏动画效果

        //1.点击箭头移动效果
        var width = $(".Bcj_nav").find("li").width();
        var length = $(".Bcj_nav").find("li").length;
        //console.log(length);
        var i = 0;
        $(".Bcj_right").on("click", function () {
            var isSwipe = true;
            i++;
            if (i >= 11) {
                i = 11;
            }
            if (isSwipe) {
                $(".Bcj_nav>ul").animate({
                    "left": "" + (-i * width) + "px",
                }, 200, "ease", function () {
                    isSwipe = false;
                })
            }
        })

        //tab栏点击请求数据，
        $(".Bcj_nav").find("li").on("click", function () {
            var id = $(this).attr("id");
            //根据点击tab栏Id请求数据
            $.ajax({
                url: URL.getbaicaijiaproduct,
                type: "get",
                data: {titleid: id},
                dataType: "json",
                success: function (data) {
                    //console.log(data);
                    var html = template("Bcj_nav_art", data);
                    $(".Bcj_goods_ul").html("");
                    $(".Bcj_goods_ul").html(html);
                },
                error: function () {
                    alert("服务器端错误，请联系管理员");
                }

            })
        })
    });
//请求首页商品数据
    BcjAjax(URL.getbaicaijiaproducttitleid,function(data){
        var html = template("Bcj_nav_art", data);
        $(".Bcj_goods_ul").html("");
        $(".Bcj_goods_ul").html(html);
    })
//tab栏滑动
    Bcjswipe();
    function Bcjswipe(){
        var startX = 0, moveX = 0, distanceX = 0, isMove = false;
        var navBox = $(".Bcj_nav ul")[0];
        var addTransition = function () {
            navBox.style.webkitTransition = "all .2s";
            /*兼容*/
            navBox.style.transition = "all .2s";
        }
        /*删除过度*/
        var removeTransition = function () {
            navBox.style.webkitTransition = "none";
            /*兼容*/
            navBox.style.transition = "mone";
        }
        /*设置定位*/
        var setTranslateX = function (x) {
            navBox.style.webkitTransform = "translateX(" + x + "px)";
            navBox.style.transform = "translateX(" + x + "px)";
        }
        navBox.addEventListener("touchstart", function (e) {
            startX = e.touches[0].clientX;
        })
        navBox.addEventListener("touchmove", function (e) {
            isMove = true;
            moveX = e.touches[0].clientX;
            distanceX = moveX - startX;
        })
        navBox.addEventListener('touchend', function (e) {
            addTransition();
            setTranslateX(distanceX)
        })
    }

    $(".Bcj_header").find("h3").click(function(){
        history.back(-1);
    })
//点击返回顶部菜单
    $(".Bcj_return").on("click", function () {
        var timer =setInterval(function(){
            var leader = document.body.scrollTop;
            var target = 0;
            var step = (target-leader)/10;
            step=step>0?Math.ceil(step):Math.floor(step);
            leader=leader+step;
            if(leader==target){
                clearInterval(timer);
            }
            document.body.scrollTop = leader;
        },15)
    });
    //窗口滚动动态改变火箭位置
    window.onscroll = function(){
        //滚动窗口,动态改变火箭位置
        var arrowDis = document.body.scrollTop;
        //console.log(arrowDis);
        $(".Bcj_returnArrow").css({
            transition:"all 1s",
            position:"absolute",
            right: "20/75rem",
            bottom:""+(-arrowDis)+"px"
        })
    }
    //返回顶部火箭
    $(".Bcj_returnArrow").on("click",function(){
        var timer =setInterval(function(){
            var leader = document.body.scrollTop;
            var target = 0;
            var step = (target-leader)/10;
            step=step>0?Math.ceil(step):Math.floor(step);
            leader=leader+step;
            if(leader==target){
                clearInterval(timer);
            }
            document.body.scrollTop = leader;
        },15)
    });

})